﻿using Prism.Events;
using Prism.Mvvm;
using RoboAssembleX.Application.Common;
using RoboAssembleX.Application.AddBoard1;
using RoboAssembleX.Application.AddGlass;
using RoboAssembleX.Application.AddGlass1;
using RoboAssembleX.Application.Robot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using RoboAssembleX.Application.AddBoard2;

namespace RoboAssembleX.Application.ActionJobs
{
    public class ActionJob01 : BindableBase,IActionJob
    {
        private readonly IEventAggregator eventAggregator;
        private readonly RoboModel roboModel;
        private readonly addGlass1Model addGlass1Model;
        private readonly RoboService roboService;
        private readonly AddGlass1Service addGlass1Service;
        private readonly AddBoard2Model addBoard2Model;

        public ActionJob01(IEventAggregator eventAggregator,RoboModel roboModel, RoboService roboService, addGlass1Model addGlass1Model,  AddGlass1Service addGlass1Service,AddBoard2Model addBoard2Model)
        {
            this.eventAggregator = eventAggregator;
            this.roboModel = roboModel;
            this.addGlass1Model = addGlass1Model;
            this.roboService = roboService;
            this.addGlass1Service = addGlass1Service;
            this.addBoard2Model = addBoard2Model;
        }
        private string name = "基片2到玻璃珠1"; public string Name { get => name; }
        private bool running; public bool Running { get => running; set { running = value; RaisePropertyChanged(); } }
        private bool enable; public bool Enable { get => enable;  set { enable = value; RaisePropertyChanged(); } }
        private int priority = 0; public int Priority { get => priority; set { priority = value; RaisePropertyChanged(); } }



        public bool IsReady
        {
            get
            {
                //玻璃珠1位未使用 玻璃珠允许放料 基片有料
                return !addGlass1Model.Pos1Used
                     && addGlass1Model.AllowPut
                     && addBoard2Model.AllowRemove && addBoard2Model.OutPosUsed;
            }
        }

        public async Task RunActionAsync()
        {
            Running = true;
            eventAggregator.GetEvent<MessageEvent>().Publish(new MessageEventArgs("启动机械臂动作1!", 1));
            await  roboService.Action1Async();
            while(roboModel.DO1)
            {
               await  Task.Delay(100);
            }
            //玻璃珠1位有料 玻璃珠2位有料 玻璃珠允许放料
            if (addGlass1Model.Pos1Used && addGlass1Model.Pos2Used && addGlass1Model.AllowPut)
            {
                eventAggregator.GetEvent<MessageEvent>().Publish(new MessageEventArgs("启动玻璃珠设备!", 1));
                addGlass1Service.Action();
            }
            await Task.Delay(100);
            Running = false;
        }
    }
}
